﻿#region 程序集 VanillaPsycastsExpanded, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null
// C:\code\MyMod\MOD\bin\this!!!!!!!1\1.4\Assemblies\VanillaPsycastsExpanded.dll
// Decompiled with ICSharpCode.Decompiler 7.1.0.6543
#endregion

using System.Collections.Generic;
using System.Linq;
using RI_Talisman;
using RimWorld;
using RimWorld.QuestGen;
using RimWorld.SketchGen;
using UnityEngine;
using Verse;

namespace WhoXiuXian
{
    public class RI_SketchResolver_AncientTalisResearchRoom : SketchResolver
    {
        private static List<IntVec3> tmpCells = new List<IntVec3>();

        private static IEnumerable<ThingDef> CentralThings
        {
            get
            {
                yield return RI_BuildingsDefOf.RI_Building_StoneSittingPlatform;
            }
        }

        private static IEnumerable<ThingDef> CornerThings
        {
            get
            {
                yield return RI_T_DefOfs.RI_Building_AncientLampA;
                yield return RI_T_DefOfs.RI_Building_AncientLampB;
                yield return RI_T_DefOfs.RI_Building_AncientLampC;
            }
        }
        private static IEnumerable<ThingDef> WallEdgeThings
        {
            get
            {
                yield return RI_BuildingsDefOf.RI_Building_HiTechResearchDesk;
                yield return RI_T_DefOfs.RI_Building_AncientTableA;
            }
        }
        protected override bool CanResolveInt(ResolveParams parms)
        {
            if (parms.rect.HasValue)
            {
                return parms.sketch != null;
            }

            return false;
        }
        private static IEnumerable<ThingDef> OtherThings
        {
            get
            {
                //yield return ThingDefOf.AncientCrate;
                //yield return ThingDefOf.AncientBarrel;
                yield return RI_T_DefOfs.RI_Building_AncientWeaponBox;
                yield return RI_T_DefOfs.RI_Building_AncientArmorBox;
            }
        }

        protected override void ResolveInt(ResolveParams parms)
        {
            CellRect rect = parms.rect.Value;
            CellRect cellRect = SketchGenUtility.FindBiggestRect(parms.sketch, (IntVec3 p) => rect.Contains(p) && !parms.sketch.ThingsAt(p).Any());
            if (cellRect == CellRect.Empty)
            {
                return;
            }

            tmpCells.Clear();
            Rot4 dir = ((cellRect.Width > cellRect.Height) ? Rot4.North : Rot4.East);
            tmpCells.AddRange(cellRect.GetEdgeCells(dir));
            tmpCells.AddRange(cellRect.GetEdgeCells(dir.Opposite));
            tmpCells.Clear();

            foreach (IntVec3 intVec3 in RI_RandomUtility.CutRectToCellListBySize(rect, 2, 2))
            {
                ThingDef thingDef = OtherThings.RandomElement();
                if (Random.Range(0, 100) > 80)
                {
                    parms.sketch.AddThing(thingDef, intVec3, Rot4.North);
                }
            }

            foreach (ThingDef cornerThing in CornerThings)
            {
                ResolveParams parms2 = parms;
                parms2.cornerThing = cornerThing;
                parms2.requireFloor = true;
                SketchResolverDefOf.AddCornerThings.Resolve(parms2);
            }
            foreach (ThingDef centralThing in CentralThings)
            {
                ResolveParams parms3 = parms;
                parms3.thingCentral = centralThing;
                parms3.requireFloor = true;
                SketchResolverDefOf.AddThingsCentral.Resolve(parms3);
            }
            foreach (ThingDef wallEdgeThing in WallEdgeThings)
            {
                ResolveParams parms4 = parms;
                parms4.wallEdgeThing = wallEdgeThing;
                parms4.allowWood = false;
                SketchResolverDefOf.AddWallEdgeThings.Resolve(parms4);
            }
            
            RI_RandomUtility.ReplaceWallAndBreakThem(rect, parms.sketch, RI_T_DefOfs.RI_Building_AncientWall, 30, 0.7f);

        }
    }
}